package com.njworkorder.Service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.njworkorder.Entity.*;
import com.njworkorder.Service.AssignmentService;
import com.njworkorder.Mapper.AssignmentMapper;
import com.njworkorder.Utils.NoCode;
import com.njworkorder.VO.AssignmentListVo;
import com.njworkorder.VO.AssignmentProgressVos.AssignmentInfo;
import com.njworkorder.VO.AwdWithDiseaseVo;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* @author iron
* @description 针对表【assignment】的数据库操作Service实现
* @createDate 2024-11-12 13:55:06
*/
@Service
public class AssignmentServiceImpl extends MPJBaseServiceImpl<AssignmentMapper, Assignment>
    implements AssignmentService{

    @Override
    public String getTaskOrderNumber() {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
        String daeF = df.format(new Date());
        LambdaQueryWrapper<Assignment> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(Assignment::getTaskOrderNumber);
        queryWrapper.likeRight(Assignment::getPublishTime, daeF);
        queryWrapper.orderByDesc(Assignment::getPublishTime);
        List<Assignment> list = this.list(queryWrapper);
        String numNew = "001";
        if (!list.isEmpty()) {
            String regex = "^[A-Z]{4}\\d{4}-\\d{2}-\\d{2}-\\d+$";
            Pattern pattern = Pattern.compile(regex);
            for (Assignment assignment : list) {
                Matcher matcher = pattern.matcher(assignment.getTaskOrderNumber());
                if (matcher.matches()) {
                    String[] split = assignment.getTaskOrderNumber().split("-");
                    String Num = split[split.length - 1];
                    numNew = NoCode.getNoCode(Num);
                    break;
                }
            }
        }
        return daeF + "-" + numNew;
    }

    @Override
    public MPJLambdaWrapper<Assignment> getMPJWrapper() {
        return new MPJLambdaWrapper<Assignment>()
                .selectAll(Assignment.class)
                .leftJoin(AdministrativeOrganization.class,AdministrativeOrganization::getId, Assignment::getMaintenanceUnitId, ext -> ext
                        .selectAs(AdministrativeOrganization::getName, AssignmentListVo::getMaintenanceUnitName))
                .leftJoin(AdministrativeOrganization.class,AdministrativeOrganization::getId, Assignment::getAuditUnitId,ext -> ext
                        .selectAs(AdministrativeOrganization::getName,AssignmentListVo::getAuditUnitName))
                .leftJoin(AdministrativePersonnel.class,AdministrativePersonnel::getId, Assignment::getConstructionManagerId, ext -> ext
                        .selectAs(AdministrativePersonnel::getName,AssignmentListVo::getConstructionManagerName))
                .leftJoin(AdministrativePersonnel.class,AdministrativePersonnel::getId, Assignment::getDispatchPersonId,ext -> ext
                        .selectAs(AdministrativePersonnel::getName,AssignmentListVo::getDispatchPersonName))
                .leftJoin(AdministrativePersonnel.class,AdministrativePersonnel::getId, Assignment::getReviewerId,ext -> ext
                        .selectAs(AdministrativePersonnel::getName,AssignmentListVo::getReviewerName))
                .leftJoin(AdministrativePersonnel.class,AdministrativePersonnel::getId,Assignment::getCreateApId,ext -> ext
                        .selectAs(AdministrativePersonnel::getName,AssignmentListVo::getCreateApName));
    }

    @Override
    public MPJLambdaWrapper<AssignmentWithDisease> getDetailMPJWrapper() {
        return new MPJLambdaWrapper<AssignmentWithDisease>()
                .selectAll(AssignmentWithDisease.class)
                .leftJoin(AdministrativeOrganization.class,AdministrativeOrganization::getId,AssignmentWithDisease::getConstructionUnitId,ext ->
                        ext.selectAs(AdministrativeOrganization::getName,AwdWithDiseaseVo::getConstructionUnitName))
                .leftJoin(AdministrativePersonnel.class,AdministrativePersonnel::getId,AssignmentWithDisease::getConstructionManagerId,ext ->
                        ext.selectAs(AdministrativePersonnel::getName,AwdWithDiseaseVo::getConstructionManagerName))
                .leftJoin(DiseaseRegistration.class,DiseaseRegistration::getId, AssignmentWithDisease::getDiseaseId,ext ->
                        ext.selectAll(DiseaseRegistration.class))
                .leftJoin(Disease.class,Disease::getId,DiseaseRegistration::getDisease,ext ->
                        ext.selectAs(Disease::getDiseaseName,AwdWithDiseaseVo::getDiseaseName))

                .leftJoin(Assignment.class,Assignment::getId,AssignmentWithDisease::getAssignmentId,ext ->
                        ext.selectAs(Assignment::getTaskOrderNumber,AwdWithDiseaseVo::getTaskOrderNumber))

                .leftJoin(InspectionRegistration.class, InspectionRegistration::getId, DiseaseRegistration::getInspectionRegistrationId, ext ->
                        ext.selectAll(InspectionRegistration.class));
    }

    @Async
    @Override
    public CompletableFuture<AssignmentInfo> getAssignmentInfo(String id) {

        MPJLambdaWrapper<Assignment> wrapper = new MPJLambdaWrapper<Assignment>()
                .selectAs(Assignment::getTaskOrderNumber,AssignmentInfo::getTaskOrderNumber)
                .selectAs(Assignment::getEstimatedEndDate,AssignmentInfo::getEstimatedEndDate)
                .selectAs(Assignment::getRemark,AssignmentInfo::getRemark)
                .selectAs(Assignment::getDispatchDate,AssignmentInfo::getDispatchDate)
                .selectAs(Assignment::getAuditTime,AssignmentInfo::getAuditTime)

                .leftJoin(AdministrativeOrganization.class,AdministrativeOrganization::getId,Assignment::getMaintenanceUnitId,ext ->
                        ext.selectAs(AdministrativeOrganization::getName,AssignmentInfo::getMaintenanceUnitName))
                .leftJoin(AdministrativeOrganization.class,AdministrativeOrganization::getId,Assignment::getAuditUnitId,ext ->
                        ext.selectAs(AdministrativeOrganization::getName,AssignmentInfo::getAuditUnitName))
                .leftJoin(AdministrativePersonnel.class,AdministrativePersonnel::getId,Assignment::getConstructionManagerId,ext ->
                        ext.selectAs(AdministrativePersonnel::getName,AssignmentInfo::getConstructionManagerName))
                .leftJoin(AdministrativePersonnel.class,AdministrativePersonnel::getId,Assignment::getReviewerId,ext ->
                        ext.selectAs(AdministrativePersonnel::getName,AssignmentInfo::getReviewerName))
                .leftJoin(AdministrativePersonnel.class,AdministrativePersonnel::getId,Assignment::getDispatchPersonId,ext ->
                        ext.selectAs(AdministrativePersonnel::getName,AssignmentInfo::getDispatchPersonName))

                .eq(Assignment::getId,id);
        return CompletableFuture.completedFuture(selectJoinOne(AssignmentInfo.class, wrapper));
    }

    @Async
    @Override
    public CompletableFuture<List<BillOfQuantities>> getBillOfQuantities(int year) {

        MPJLambdaWrapper<Assignment> wrapper = new MPJLambdaWrapper<Assignment>()
                .select(BillOfQuantities::getNums,BillOfQuantities::getUnitPrice)
                .selectAs("DATE_FORMAT(t.publish_time,'%Y-%m')",BillOfQuantities::getRemark)

                .leftJoin(BillOfQuantities.class,BillOfQuantities::getJobManagementId,Assignment::getId)
                .leftJoin(PlanMetering.class,PlanMetering::getId,BillOfQuantities::getMcId)
                .leftJoin(Plan.class,Plan::getId,PlanMetering::getPlanId)

                .eq(Assignment::getAuditStatus,7)
                .apply("YEAR(t.publish_time) = " + year);

        return CompletableFuture.completedFuture(selectJoinList(BillOfQuantities.class, wrapper));
    }


}




